<?php

/**
 * @author ict.sonla.gov.vn
 * @copyright 2012
 */

defined('DSVH') or die('Restricted access');

//G?i t?p tin giao di?n trong thu m?c view/theme/template tuong ?ng
 if(!$_SESSION['loged']){
         header('LOCATION: index.php?module=canbo&act=login');  
         exit();
     }
include('controller/c_thongke.php');     
include ('model/m_dantoc.php');
$page = $_GET['act'];
 switch($page){
    case 'list':
    //Get language
    require('language/dantoc/list_dantoc.php');
    $heading_title = heading_title;
    $button_add = button_add;
    $button_edit = button_edit;
    $button_delete = button_delete;
    
    $ma_dan_toc = ma_dan_toc;
	$ten_dan_toc = ten_dan_toc;
    $thao_tac = thao_tac;
    $link_add ='index.php?module=dantoc&act=add';
    
      if($_POST['selected']==""){
        $err['exist'] = chua_chon;
        $error_name = $err['exist'];
        }
    if(!$err){
        $valid = true;    
    }
    else{
        $valid = false;
    }
    //kiem tra du lieu cua from
    if(($_SERVER['REQUEST_METHOD']=='POST')&&$valid){
      foreach($_POST['selected'] as $id){
        $dantoc = getDantoc($id);
         if($dantoc['avatar']){
           unlink('./data/image/dantoc/'.$dantoc['avatar']);
         }
        
         $rangbuoc = checkcanbo($id);
         $rbpvt = check_hoso_pvt($id);
         if($rbpvt >= 1){
            $loi['pvt'] ='loi';
         }
         if($rangbuoc >= 1){
            $loi['canbo'] = 'loi';
         }
         
         if(!$loi){
            
         deldantoc($id);
         
         $_SESSION['success'] = del_success;
         
         }else{
            
          $_SESSION['error'] = khong_the_xoa_du_lieu_nay;
          $error_warning =  khong_the_xoa_du_lieu_nay;
          
         }
        
      }  
   
    //chuyen huong ve list
    header('LOCATION: '.$link_list);
     }
 
    $list_dantoc = getListDantoc();
    
    require('view/template/dantoc/list_dantoc.tpl');
    break;
	
	//case them chuc danh
	case 'add':
	require('language/dantoc/add_dantoc.php');
	$heading_title = heading_title;
    $ma_dan_toc = ma_dan_toc;
	$ten_dan_toc = ten_dan_toc;
    $motachitiet = mo_ta_chi_tiet;
    $motachung = mo_ta_chung;
    $hinhdaidien = hinh_dai_dien;
    
    $link_list = 'index.php?module=dantoc&act=list';
    
    if($_POST['ten_dan_toc']==""){
        $err['exist'] = chua_nhap_ten;
        $error_name = $err['exist'];
             }
    if(!$err){
        $valid = true;    
    }
    else{
        $valid = false;
    }
    if(($_SERVER['REQUEST_METHOD']=='POST')&&$valid)
    {
        if ((($_FILES["file"]["type"] == "image/gif")||($_FILES["file"]["type"] == "image/jpeg")||($_FILES["file"]["type"] == "image/pjpeg"))&& ($_FILES["file"]["size"] < 2000000))
        {
              if ($_FILES["file"]["error"] > 0)
                {
                  $_SESSION['invalid_file'] = 'invalid file';
                  Header("Location: ".$dr);
                }else
                {
                    $name = make_picture_name($_POST['ten_dan_toc']);
                    $ext = end(explode(".",$_FILES["file"]["name"]));
                    $file_name = $name.".".$ext;
                    
                    if (file_exists("./data/image/dantoc/".$file_name))
                      {
                  	    unlink('./data/image/dantoc/'.$file_name);
                         move_uploaded_file($_FILES["file"]["tmp_name"],'./data/image/dantoc/'.$file_name);
                      }
                    else
                      {
                        move_uploaded_file($_FILES["file"]["tmp_name"],"./data/image/dantoc/".$file_name);
                      //move_uploaded_file($_FILES["file"]["tmp_name"], "../images/local/".$file_name);
                      
                      }
                }
        }else
        {
                   $_SESSION['invalid_file'] = 'invalid file';
                   Header("Location: ".$dr);
        }
                  
    $_POST['avatar'] = $file_name;   
    insertDantoc($_POST);
    $_SESSION['success'] = edit_success;
    //chuyen huong ve list
    header('LOCATION: '.$link_list);    
    }
    require('view/template/dantoc/add_dantoc.tpl');
    break;
    
    //case sua dan toc
    case 'edit':
    require('language/dantoc/edit_dantoc.php');
    $heading_title = heading_title;
    $ma_dan_toc = ma_dan_toc;
    $ten_dan_toc = ten_dan_toc;
    $motachitiet = mo_ta_chi_tiet;
    $motachung = mo_ta_chung;
    $hinhdaidien = hinh_dai_dien;
    $link_list = 'index.php?module=dantoc&act=list';
    $id = $_REQUEST['id'];
    $dantoc = getDantoc($id);
    $_POST['avatar'] = $dantoc['avatar'];
    
    if($_SERVER['REQUEST_METHOD']=='POST')
    {
      if ((($_FILES["file"]["type"] == "image/gif")||($_FILES["file"]["type"] == "image/jpeg")||($_FILES["file"]["type"] == "image/pjpeg"))&& ($_FILES["file"]["size"] < 2000000))
        {
              if ($_FILES["file"]["error"] > 0)
                {
                  $_SESSION['invalid_file'] = 'invalid file';
                  Header("Location: ".$dr);
                }else
                {
                    $name = make_picture_name($_POST['ten_dan_toc']);
                    $ext = end(explode(".",$_FILES["file"]["name"]));
                    $file_name = $name.".".$ext;
                    
                    if (file_exists("./data/image/dantoc/".$file_name))
                      {
                  	    unlink('./data/image/dantoc/'.$file_name);
                        move_uploaded_file($_FILES["file"]["tmp_name"],'./data/image/dantoc/'.$file_name);
                          $_POST['avatar'] = $file_name;
                      }
                    else
                      {
                        move_uploaded_file($_FILES["file"]["tmp_name"],'./data/image/dantoc/'.$file_name);
                      //move_uploaded_file($_FILES["file"]["tmp_name"], "../images/local/".$file_name);
                        $_POST['avatar'] = $file_name;
                      }
                }
        }else
        {
                   $_SESSION['invalid_file'] = 'invalid file';
                   Header("Location: ".$dr);
        }
      
    editDantoc($id, $_POST);
    $_SESSION['success'] = edit_success;
    //chuyen huong ve list
    header('LOCATION: '.$link_list);    
    }
        
    
    require('view/template/dantoc/edit_dantoc.tpl');
    break; 
    
    case 'del':
    get_file('language/dantoc/del_dantoc.php');
    $link_list = 'index?module=dantoc&act=list';
    $id = $_REQUEST['id'];
    $dantoc = getDantoc($id);
    if($dantoc['avatar']){
           unlink('./data/image/dantoc/'.$dantoc['avatar']);
         }
    deldantoc($id);
     $_SESSION['success'] = del_success;
    //chuyen huong ve list
     header('LOCATION: '.$link_list);  
     exit();
         
    
    break;  
}
?>